package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.CcBerthDAO;
import com.trkj.dao.jpa.CcGenreDAO;
import com.trkj.dao.jpa.CcHospFloorDAO;
import com.trkj.dao.jpa.CcStoreyDAO;
import com.trkj.dao.jpa.CcWardDAO;
import com.trkj.dao.jpa.TbdepartmentDAO;
import com.trkj.pojos.CcBerth;
import com.trkj.pojos.CcGenre;
import com.trkj.pojos.CcStorey;
import com.trkj.pojos.CcWard;
import com.trkj.pojos.Tbdepartment;
import com.trkj.vo.CcBerthVo;
import com.trkj.vo.CcStoreyVo;
import com.trkj.vo.CcWardVo;

@Service
@Transactional
public class CcWardService {
	@Autowired
	private CcWardDAO ccwardDAO;
	@Autowired
	private CcHospFloorDAO ccHospDAO;
	@Autowired
	private CcGenreDAO ccGDAO;
	@Autowired
	private CcStoreyDAO ccSDAO;
	@Autowired
	private TbdepartmentDAO tbDAO;

	@Autowired
	private CcBerthDAO cbDAO;

	public List<CcGenre> queryAllCcGenre() {
		return (List<CcGenre>) ccGDAO.findAll();
	}

	public CcWard findByccwardId(long wid) {
		return ccwardDAO.findOne(wid);
	}

	public void updateccward(CcWard cw) {
		CcWard cwd = ccwardDAO.findOne(cw.getWardid());
		if (cwd != null) {
			cwd.setHremarks(cw.getHremarks());
			cwd.setWmoney(cw.getWmoney());
			cwd.setWberth(cwd.getWberth());
			cwd.setWgenreid(cw.getWgenreid());
			cwd.setWname(cw.getWname());
			cwd.setWofficesId(cw.getWofficesId());
			cwd.setWpeople(cwd.getWpeople());
			cwd.setWstoId(cw.getWstoId());
			ccwardDAO.save(cwd);
		}

	}

	public void addWard(String wname, String wberth, String wmoney, String wofficesId, String wgenreid, String wstoId) {
		CcWard cw = new CcWard();
		cw.setHremarks("未住满");
		cw.setWberth(wberth);
		cw.setWpeople(0 + "");
		cw.setWname(wname);
		cw.setWgenreid(new BigDecimal(wgenreid));
		cw.setWofficesId(new BigDecimal(wofficesId));
		cw.setWmoney(new BigDecimal(wmoney));
		cw.setWstoId(new BigDecimal(wstoId));
		ccwardDAO.save(cw);
		int b = Integer.parseInt(wberth);
		for (int i = 0; i < b; i++) {
			addBerth(cw.getWardid());
		}

	}

	public void addBerth(long wid) {
		CcBerth cb = new CcBerth();
		cb.setBerthstate("空");
		cb.setBwardno(new BigDecimal(wid));
		cbDAO.save(cb);
		cb.setBname(wid + "-" + cb.getBerthno());
		cbDAO.save(cb);
	}

	public void addWardBerth(String wid) {
		CcBerth cb = new CcBerth();
		cb.setBerthstate("空");
		cb.setBwardno(new BigDecimal(wid));
		cbDAO.save(cb);
		cb.setBname(wid + "-" + cb.getBerthno());
		CcWard ccward = ccwardDAO.findOne(Long.valueOf(wid));
		if (ccward.getWberth() != null) {
			ccward.setWberth(Integer.parseInt(ccward.getWberth()) + 1 + "");
		} else {
			ccward.setWberth(1 + "");
		}
		ccward.setHremarks("未住满");
		ccwardDAO.save(ccward);
		cbDAO.save(cb);
	}

	public void delWardBerth(Long[] bid) {
		for (int i = 0; i < bid.length; i++) {
			CcBerth cb = cbDAO.findOne(bid[i]);
			CcWard ccward = ccwardDAO.findOne(cb.getBwardno().longValue());
			ccward.setWberth(Integer.parseInt(ccward.getWberth()) - 1 + "");
			cbDAO.delete(cb);
			ccwardDAO.save(ccward);

		}
	}

	public Map<String, Object> queryAllWard(int page, int rows) {
		PageRequest pr = new PageRequest(page - 1, rows);
		// 获取当前页的记录
		List<CcWard> list = ccwardDAO.findAll(pr);
		// 获取总的记录条数

		Map<String, Object> map = new HashMap<>();
		map.put("total", ccwardDAO.count());
		map.put("rows", poWardList2VoList(list));
		return map;
	}
	// ccVo.setWeizhurenshu(Integer.parseInt(cw.getWberth())
	// -Integer.parseInt(cw.getWpeople()));

	public Map<String, Object> queryWardHremarks(int page, int rows) {
		PageRequest pr = new PageRequest(page - 1, rows);
		// 获取当前页的记录
		List<CcWard> list = ccwardDAO.findByHremarks(pr);
		// 获取总的记录条数
		long total = ccwardDAO.findBycount();

		Map<String, Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", poWardList2VoList(list));
		return map;
	}

	public List<CcGenre> findByGename(String gename) {
		return ccGDAO.findByGename(gename);
	}

	// 根据病房类别查询
	public List<CcWardVo> queryBywardGename(String gename) {
		List<CcGenre> ccg = findByGename(gename);
		List<CcWardVo> ccw = new ArrayList<>();
		List<CcWard> cw = new ArrayList<>();
		for (CcGenre cg : ccg) {
			List<CcWard> lw = ccwardDAO.findByWgenreid(new BigDecimal(cg.getGenreid()));
			for (CcWard cc : lw) {
				CcWard e = new CcWard();
				e.setHremarks(cc.getHremarks());
				e.setWardid(cc.getWardid());
				e.setWberth(cc.getWberth());
				e.setWgenreid(cc.getWgenreid());
				e.setWmoney(cc.getWmoney());
				e.setWname(cc.getWname());
				e.setWofficesId(cc.getWofficesId());
				e.setWpeople(cc.getWpeople());
				e.setWstoId(cc.getWstoId());
				cw.add(e);
			}
		}
		ccw = poWardList2VoList(cw);
		return ccw;
	}

	// 根据所属科室查询
	public List<CcWardVo> queryBywarddepartmentName(String departmentName) {
		List<Tbdepartment> tb = querydepartmentName(departmentName);
		List<CcWardVo> ccw = new ArrayList<>();
		List<CcWard> cw = new ArrayList<>();
		for (Tbdepartment cg : tb) {
			List<CcWard> lw = ccwardDAO.findByWofficesId(new BigDecimal(cg.getDepartmentNo()));
			for (CcWard cc : lw) {
				CcWard e = new CcWard();
				e.setHremarks(cc.getHremarks());
				e.setWardid(cc.getWardid());
				e.setWberth(cc.getWberth());
				e.setWgenreid(cc.getWgenreid());
				e.setWmoney(cc.getWmoney());
				e.setWname(cc.getWname());
				e.setWofficesId(cc.getWofficesId());
				e.setWpeople(cc.getWpeople());
				e.setWstoId(cc.getWstoId());
				cw.add(e);
			}
		}
		ccw = poWardList2VoList(cw);
		return ccw;
	}

	// 病房名称查询
	public List<CcWardVo> queryBywardwname(String wname) {
		List<CcWard> list=ccwardDAO.findByWname(wname);
		return poWardList2VoList(list);
	}

	public List<Tbdepartment> querydepartmentName(String departmentName) {
		return tbDAO.queryByName(departmentName);
	}

	private List<CcWardVo> poWardList2VoList(List<CcWard> list) {
		if (list == null)
			return null;
		List<CcWardVo> listVo = new ArrayList<>();
		for (CcWard cw : list) {
			CcWardVo cwVo = new CcWardVo();
			// stuVo.setAddress(stu.getAddress());
			// stuVo.setPwd(stu.getPwd());
			cwVo.setWardid(cw.getWardid());
			cwVo.setHremarks(cw.getHremarks());
			cwVo.setWname(cw.getWname());
			cwVo.setWberth(cw.getWberth());
			cwVo.setWmoney(cw.getWmoney());
			cwVo.setWpeople(cw.getWpeople());
			cwVo.setGenreid(cw.getWgenreid().longValue());

			cwVo.setGename(ccGDAO.queryName(cwVo.getGenreid()));

			cwVo.setStorey_id(cw.getWstoId().longValue());
			cwVo.setSto_name(ccSDAO.queryName(cwVo.getStorey_id()));

			cwVo.setDepartmentNo(cw.getWofficesId().longValue());
			cwVo.setDepartmentName(tbDAO.queryName(cwVo.getDepartmentNo()));
			CcStoreyVo cc = new CcStoreyVo();
			CcStorey cs = ccSDAO.findOne(cwVo.getStorey_id());
			cc.setFloor_id(cs.getSfloorId().longValue());
			cwVo.setFloor_id(cc.getFloor_id());
			cwVo.setFloor_name(ccHospDAO.queryName(cwVo.getFloor_id()));
			cwVo.setAddress(cwVo.getFloor_name() + cwVo.getSto_name());
			if (cw.getWpeople() != null) {
				cwVo.setWeizhurenshu(Integer.parseInt(cw.getWberth()) - Integer.parseInt(cw.getWpeople()));
			} else {
				cwVo.setWeizhurenshu(Integer.parseInt(cw.getWberth()));
			}
			listVo.add(cwVo);
		}
		return listVo;
	}
}
